from BaseHTTPServer import *
from SimpleHTTPServer import *
import glob,sys,os

from optparse import OptionParser

oneshot = True # not yet


class OneShotHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        print "getting", `self.path`
        p = self.path[1:]
        if p not in allfiles:
            self.send_error(404, "File not found")
            return
        # delegate
        SimpleHTTPRequestHandler.do_GET(self)
        if oneshot:
            print "Delivered",p
            allfiles.remove(p)
            print "Remaining in share shared:",allfiles

def main():
    parser = OptionParser()
    parser.add_option("-p", "--port", dest="port", default = '9999',
                      help="Port number", metavar="PORT")
    parser.add_option("-a", "--addr",
                      dest="host", default='localhost', metavar = "HOST",
                      help="Specify hostname for copy-pasting convenience")


    (options, args) = parser.parse_args()

    global allfiles
    if len(args) > 0:
        allfiles = glob.glob(args[0])
    else:
        print "Sharing all of folder",os.getcwd()
        allfiles = os.listdir('.')

    print '\n'.join('http://%s:%s/%s' % (options.host, options.port, f) for f in allfiles)
        

    HTTPServer(('',int(options.port)), OneShotHandler).serve_forever()
    
if __name__ == '__main__':
    main()